﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class Cargo_Search_View : Form
    {
        public static DataSet ds = null;     // 用来保存返回的数据库
        public Cargo_Search_View()
        {
            InitializeComponent();
        }

        private void Cargo_Search_View_Load(object sender, EventArgs e)
        {
            Set_Color(dataGridView1);
        }

        private void Set_Color(DataGridView datagridview1)
        {
            for (int i = 0; i < datagridview1.RowCount; i++)
            {
                for (int j = 0; j < datagridview1.ColumnCount; j++)
                {
                    if (i % 2 == 0) datagridview1[j, i].Style.BackColor = Color.LightBlue;
                }
            }
        }

        public void Fill_Grid_From_DS(DataSet ds,DataGridView datagridview1)
        {
            try
            {
                // 将表格清空
                datagridview1.RowCount = 1;
                int colnum = datagridview1.ColumnCount;
                
                for(int i=0;i<colnum;i++)
                {
                    datagridview1[i, 0].Value = "";
                }

                int ds_rowcount = ds.Tables[0].Rows.Count;
                int ds_colcount = ds.Tables[0].Columns.Count;
                datagridview1.RowCount = ds_rowcount;
                datagridview1.ColumnCount = ds_colcount;
                for(int i=0;i<ds_rowcount;i++)
                {
                    for(int j=0;j<ds_colcount;j++)
                    {
                        datagridview1[j, i].Value = ds.Tables[0].Rows[i][j].ToString();
                    }
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("Select * from cargotable");
                Fill_Grid_From_DS(ds,dataGridView1);
                int count = ds.Tables[0].Rows.Count;
                label2.Text = "符合条件的货物有"+count.ToString()+"个";

            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Tiaojiansousuo newfrm = new Tiaojiansousuo();
            newfrm.ShowDialog();
            if(newfrm.DialogResult==DialogResult.OK)
            {
                Fill_Grid_From_DS(Tiaojiansousuo.returndataset,dataGridView1);
                int count = Tiaojiansousuo.returndataset.Tables[0].Rows.Count;
                label2.Text = "符合条件的货物有" + count.ToString() + "个";
            }
            
        }
    }
}
